package com.one.controller;

import com.one.constant.RabbitMQConstant;
import com.one.constant.SystemConstant;
import com.one.producer.RabbitMQProducer;
import com.one.service.ProductService;
import com.one.util.RedisUtils;
import com.one.util.RequestReturn;
import com.one.util.RequestReturnFactory;
import com.one.utils.JedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @ClassName: StreetSubscribeController
 * @Auther: yph
 * @Date: 2022/1/16 21:37
 * @Description: 用户订阅到货通知， 到货通知
 */
@Slf4j
@RestController
@RequestMapping("/product")
public class StreetSubscribeController {
    @Autowired
    private ProductService productService;

    @Autowired
    private RabbitMQProducer rabbitMQProducer;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private JedisUtil jedisUtil;

    /**
     * @Author: yph
     * @Date: 2022/1/3 13:58
     * @return:
     * @Description: 对订阅 商品id的用户发送消息
     */
    @GetMapping("/addProduct")
    public RequestReturn<Map<String,Object>> testRabbitMQDirectExchange(@RequestParam("productId") Integer productId,@RequestParam("addNum") Integer addNum) {
        log.info("------ 发送消息到交换机 begin");
        //调用直连消息生产者，发送消息到直连交换机
        rabbitMQProducer.sendDirectMsg2DirectQueue(RabbitMQConstant.RABBITMQ_DIRECT_EXCHANGE_PRODUCT, RabbitMQConstant.RABBITMQ_DIRECT_ROUTING_PRODUCT, productId);
        log.info("------ 发送消息到交换机 end");
//        return RequestReturnFactory.success("商品 [" + productService.queryProductNameById(productId) + "] 已到货");
        return productService.addProductStock(productId,addNum);
    }

    /**
     * @Author: yph
     * @Date: 2022/1/16 21:36
     * @return:
     * @Description: 订阅商品到货提醒
     */
    @GetMapping("/subscribeAddProduct")
    public RequestReturn<String> subscribeAddProduct(@RequestParam("phone") String phone, @RequestParam("productId") Integer productId) {
        // 判断是否存在该用户，如果存在，就取消
        if (redisUtils.hHasKey(RabbitMQConstant.SUBSCRIBE_PRODUCT_PREFIX + productId, phone))
            return RequestReturnFactory.success("已订阅 [" + productService.queryProductNameById(productId) + "]到货提醒!");

        // 不存在就添加到redis中
        redisUtils.hset(RabbitMQConstant.SUBSCRIBE_PRODUCT_PREFIX + productId, phone, 0);
        return RequestReturnFactory.success("订阅[" + productService.queryProductNameById(productId) + "]到货提醒成功!");
    }

    /**
     * @Author: yph
     * @Date: 2022/1/16 21:36
     * @return:
     * @Description: 订阅商品到货提醒
     */
    @GetMapping("/deleteAddProduct")
    public Long deleteAddProduct(@RequestParam("phone") String phone,
                                 @RequestParam("productId") Integer productId) {
        return jedisUtil.del(RabbitMQConstant.SUBSCRIBE_PRODUCT_PREFIX + productId);
    }

    /**
     * @Author: yph
     * @Date: 2022/1/18 15:12
     * @return:
     * @Description: 根据手机号查询收到的消息
     */
    @GetMapping("/receiveMessage")
    public RequestReturn<String> receiveMessage(@RequestParam("phone") String phone) {
        String message = jedisUtil.get("sendMessage:" + phone);
        if (message == null) {
            return RequestReturnFactory.success("当前没有收到消息");
        }
        return RequestReturnFactory.success(message);
    }
    
    /**
     * @Author: yph
     * @Date: 2022/1/22 01:15
     * @return: 
     * @Description:  根据商品id 查看商品基本信息
    */
    @GetMapping("/queryProductStock")
    public RequestReturn<Map<String,Object>> queryProductStock(@RequestParam("productId") Integer productId){
        return productService.queryProductStock(productId);
    }


}
